package com.admin.propertistoreadmin;

import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.HttpClient;
import org.apache.http.client.ResponseHandler;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.BasicResponseHandler;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;
import org.json.JSONArray;
import org.json.JSONObject;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.ProgressDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

public class LoginAdminActivity extends Activity {
	private JSONArray jsonArray;
	private JSONObject jsonObject;
	private Streamer streamer;
	private String process = "login";
	private EditText txtUser, txtPass;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_login_admin);
		txtUser = (EditText) findViewById(R.id.txtUser);
		txtPass = (EditText) findViewById(R.id.txtPass);
		streamer = new Streamer();
	}

	public void onLogin(View v) {
		if (txtUser.getText().toString().equals("")) {
			Toast.makeText(this, "Username harus di isi", Toast.LENGTH_SHORT)
					.show();
		} else if (txtPass.getText().toString().equals("")) {
			Toast.makeText(this, "Password harus di isi", Toast.LENGTH_SHORT)
					.show();
		} else if (txtPass.getText().toString().equals("")
				&& txtUser.getText().toString().equals("")) {
			Toast.makeText(this, "Username dan Password harus di isi",
					Toast.LENGTH_SHORT).show();
		} else {
			new Asyncronous().execute("login");
		}
	}

	public void onTutup(View v) {
		exit();
	}

	private void checkUser() {
		try {
			List<NameValuePair> nvp = new ArrayList<NameValuePair>();
			nvp.add(new BasicNameValuePair("action", "login"));
			nvp.add(new BasicNameValuePair("username",txtUser.getText().toString()));
			
			nvp.add(new BasicNameValuePair("password",txtPass.getText().toString()));
			
			jsonArray = streamer
					.postData(VariabelGlobal.url + "/user.php", nvp);
			jsonObject = jsonArray.getJSONObject(0);

		} catch (Exception e) {
			// TODO: handle exception
		}
	}

	private void result() throws Exception{
		
		try {
			String resultLogin = jsonObject.getString("status");
			if (resultLogin.equals("welcome")) {
				Toast.makeText(this, jsonObject.getString("status"),
						Toast.LENGTH_SHORT).show();
				startActivity(new Intent(LoginAdminActivity.this,
						MenuUtamaActivity.class));
			} else {
				Toast.makeText(this, jsonObject.getString("status"),
						Toast.LENGTH_SHORT).show();
			}
		} catch (Exception e) {
			// TODO: handle exception
		} 
	}

	public void onBackPressed() {
		exit();
	}

	private void exit() {
		AlertDialog.Builder builder = new AlertDialog.Builder(this);
		builder.setMessage("Anda Yakin ingin keluar?")
				.setCancelable(false)
				.setPositiveButton("Ya", new DialogInterface.OnClickListener() {
					public void onClick(DialogInterface dialog, int id) {
						finish();
					}
				})
				.setNegativeButton("Tidak",
						new DialogInterface.OnClickListener() {
							public void onClick(DialogInterface dialog, int id) {
								dialog.cancel();
							}
						}).show();
	}

	private class Asyncronous extends AsyncTask<Object, Integer, Void> {
		private ProgressDialog dialog;

		@Override
		protected void onPreExecute() {
			dialog = new ProgressDialog(LoginAdminActivity.this);
			dialog.setMessage("Authenticating...");
			dialog.setIndeterminate(false);
			dialog.setCanceledOnTouchOutside(false);
			dialog.setCancelable(false);
			dialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);
			dialog.setProgress(0);
			dialog.show();
		}

		@Override
		protected Void doInBackground(Object... arg0) {
			if (arg0[0].equals("login")) {
				process = "login";
				checkUser();
			}
			return null;
		}

		@Override
		protected void onProgressUpdate(Integer... progress) {
			dialog.setProgress(progress[0]);
		}

		@Override
		protected void onPostExecute(Void result) {
			try {
				dialog.dismiss();
				if (process.equals("login")) {
					result();
				}
			} catch (Exception e) {
				e.printStackTrace();
			}

		}

	}
}
